<?php

namespace backend\services\home;

use backend\extensions\ApiBaseService;
use common\models\order\OrderModel;
use common\models\user\UserModel;
use common\models\recharge\RechargeOrderModel;
use common\models\order\OrderGoodsModel;

/**
 * Desc 首页统计服务类
 * @author HUI
 */
class HomeService extends ApiBaseService {

    /**
     * 首页统计
     * * */
    public static function index() {
        $data = [
            'sales' => self::sales(),
            'member' => self::member(),
            'undeliver' => OrderModel::find()->where(['state'=> OrderModel::STATE_WAIT_DELIVERY])->count(),
            'week_sales' => self::weekSales(),
            'week_member' => self::weekMember()
        ];
        return $data;
    }

    /**
     * 销售数据
     * * */
    private static function sales() {
        //今日
        $today_start = date('Y-m-d 00:00:00');
        $today_end = date('Y-m-d 23:59:59');

        //本周
        $week = date('w', strtotime($today_start));
        $week_start = date('Y-m-d', strtotime("-{$week} days", strtotime($today_start)));
        $week_end = date('Y-m-d', strtotime("+" . (6 - $week) . " days", strtotime($today_start)));

        //本月
        $month_start = date('Y-m-01 00:00:00');
        $month_end = date('Y-m-d 23:59:59');
        $order = OrderModel::find()
                ->select([
                    "ifnull(sum(if(state=2 or state=3 or state=4,payment_amount,0)),0) as payment_amount_total",
                    "ifnull(sum(if(create_time >= '{$today_start}' and create_time<= '{$today_end}' and (state=2 or state=3 or state=4),payment_amount,0)),0) as today_payment_amount_total",
                    "ifnull(sum(if(create_time >= '{$today_start}' and create_time<= '{$today_end}' and (state=2 or state=3 or state=4),1,0)),0) as today_order_count",
                    "ifnull(sum(if(create_time >= '{$today_start}' and create_time<= '{$today_end}' and state=6,1,0)),0) as today_refund_count",
                    "ifnull(sum(if(create_time >= '{$week_start}' and create_time<= '{$week_end}' and (state=2 or state=3 or state=4),payment_amount,0)),0) as thisweek_payment_amount_total",
                    "ifnull(sum(if(create_time >= '{$month_start}' and create_time<= '{$month_end}' and (state=2 or state=3 or state=4),payment_amount,0)),0) as thismonth_payment_amount_total",
                ])
                ->where(['state' => [2, 3, 4, 6, 7]])
                ->asArray()
                ->one();
        $goods = OrderGoodsModel::find()
                ->alias('g')
                ->leftJoin(OrderModel::tableName() . ' o', 'o.order_sn = g.order_sn')
                ->where(['AND', ['>=', 'o.create_time', $today_start], ['<=', 'o.create_time', $today_end], ['in', 'o.state', [2, 3, 4, 7]]])
                ->sum('g.count');
        $order['today_goods_count'] = $goods;
        return $order;
    }

    /**
     * 会员数据
     * * */
    private static function member() {
        //今日
        $today_start = date('Y-m-d 00:00:00');
        $today_end = date('Y-m-d 23:59:59');

        //本周
        $week = date('w', strtotime($today_start));
        $week_start = date('Y-m-d', strtotime("-{$week} days", strtotime($today_start)));
        $week_end = date('Y-m-d', strtotime("+" . (6 - $week) . " days", strtotime($today_start)));

        //本月
        $month_start = date('Y-m-01 00:00:00');
        $month_end = date('Y-m-d 23:59:59');

        return UserModel::find()
                        ->select([
                            "ifnull(sum(1),0) as member_count",
                            "ifnull(sum(if(create_time >= '{$today_start}' and create_time<= '{$today_end}' ,1,0)),0) as today_member_count",
                            "ifnull(sum(if(create_time >= '{$week_start}' and create_time<= '{$week_end}' ,1,0)),0) as thisweek_member_count",
                            "ifnull(sum(if(create_time >= '{$month_start}' and create_time<= '{$month_end}' ,1,0)),0) as thismonth_member_count",
                        ])
                        ->asArray()
                        ->one();
    }

    // /**
    //  * 充值数据
    //  * * */
    // private static function recharge() {
    //     //今日
    //     $today_start = date('Y-m-d 00:00:00');
    //     $today_end = date('Y-m-d 23:59:59');

    //     //本周
    //     $week = date('w', strtotime($today_start));
    //     $week_start = date('Y-m-d', strtotime("-{$week} days", strtotime($today_start)));
    //     $week_end = date('Y-m-d', strtotime("+" . (6 - $week) . " days", strtotime($today_start)));

    //     //本月
    //     $month_start = date('Y-m-01 00:00:00');
    //     $month_end = date('Y-m-d 23:59:59');

    //     return RechargeOrderModel::find()
    //                     ->select([
    //                         "ifnull(sum(if(create_time >= '{$today_start}' and create_time<= '{$today_end}' ,payment_amount,0)),0) as today_payment_amount_total",
    //                         "ifnull(sum(if(create_time >= '{$week_start}' and create_time<= '{$week_end}' ,payment_amount,0)),0) as thisweek_payment_amount_total",
    //                         "ifnull(sum(if(create_time >= '{$month_start}' and create_time<= '{$month_end}' ,payment_amount,0)),0) as thismonth_payment_amount_total",
    //                     ])
    //                     ->where(['state' => [2, 3, 4, 5, 6]])
    //                     ->asArray()
    //                     ->one();
    // }

    /**
     * 获取7天的销售数据
     * * */
    private static function weekSales() {

        //七天的数据
        $start_time = strtotime(date('Y-m-d 00:00:0',strtotime('-6 days')));
        $end_time= strtotime(date('Y-m-d 23:59:59',strtotime('-1 days')));
        
        //初始化数据
        $data[date('Y-m-d',$start_time)] = 0.00;
        while ($start_time < $end_time){
            $start_time = strtotime('+1 day',$start_time);
            $data[date('Y-m-d',$start_time)] = 0.00;
        }
        $start = date('Y-m-d 00:00:00', strtotime('-6 days'));
        $end = date('Y-m-d 23:59:59');
        $order = OrderModel::find()
                ->where(['AND', ['>=', 'create_time', $start], ['<=', 'create_time', $end], ['in', 'state', [2, 3, 4, 7]]])
                ->asArray()
                ->all();
        foreach ($order as $value) {
            $data[date('Y-m-d', strtotime($value['create_time']))] += $value['payment_amount'];
        }
        return ['key'=> array_keys($data),'value'=> array_values($data)];
    }
    
    /**
     * 获取7天的用户数据
     * * */
    private static function weekMember() {

        //七天的数据
        $start_time = strtotime(date('Y-m-d 00:00:0',strtotime('-6 days')));
        $end_time= strtotime(date('Y-m-d 23:59:59',strtotime('-1 days')));
        
        //初始化数据
        $data[date('Y-m-d',$start_time)] = 0.00;
        while ($start_time < $end_time){
            $start_time = strtotime('+1 day',$start_time);
            $data[date('Y-m-d',$start_time)] = 0.00;
        }
        $start = date('Y-m-d 00:00:00', strtotime('-6 days'));
        $end = date('Y-m-d 23:59:59');
        $order = UserModel::find()
                ->where(['AND', ['>=', 'create_time', $start], ['<=', 'create_time', $end]])
                ->asArray()
                ->all();
        foreach ($order as $value) {
            $data[date('Y-m-d', strtotime($value['create_time']))] += 1;
        }
        return ['key'=> array_keys($data),'value'=> array_values($data)];
    }

}
